<?php
/**
 * Created by JetBrains PhpStorm.
 * File: MenuItemAction.class.php
 * User: Xnfy520@gmail.com
 * Date: 12-3-15
 * Time: 下午6:10
 */


class MenuItemAction extends CommonAction{

	//菜单列表
	function index(){

		$menuitem = M('MenuItem');
		import("ORG.Util.page");
			unset($_SESSION['miid']);
			if(isset($_GET['mid']) && !empty($_GET['mid'])){
				$count = $menuitem->where('menuid='.$_GET['mid'])->count();
				if($count<C('PAGENUM')){
					$pagedata = array(1);
				}else{
					$pagedata = array(0,1,2,3,4,5,7,8);
				}
				$page = new page($count, C('PAGENUM'));
				$show = $page->fpage($pagedata);
				$field = array('id', 'name', 'pid', 'path', "concat(path, '-', id)"=>'bpath', 'type');
				$list = $menuitem->field($field)->where('menuid='.$_GET['mid'])->order('bpath, id')->limit($page->getlimitfirst().','.C('PAGENUM'))->select();
					foreach($list as $key=>$value){
						$list[$key]['count'] = count(explode('-', $value['path']));
						for($i=0; $i<$list[$key]['count']; $i++){
							$list[$key]['nbsp'].= "&nbsp;&nbsp;";
						}
					}
				if($count==0){
					$this->assign('isshow', 'no');
					$this->assign('nobody', 'no');
				}else{
					$this->assign('isshow', 'yes');
					$this->assign('nobody', 'yes');
				}
			}else{
				$this->error('异常操作');
			}

		$this->assign('show', $show);
		$this->assign('list', $list);
		$this->display('index');
	}

	//指向添加菜单界面
	function add(){
		if((isset($_POST['mid']) && !empty($_POST['mid'])) || (isset($_GET['mid']) && !empty($_GET['mid']))){
			if($_GET['adds']=="custom" || (isset($_POST['cid']) && !empty($_POST['cid']))){
				if($_GET['adds']=="custom" || (isset($_POST['sid']) && !empty($_POST['sid']))){
					unset($_SESSION['cid']);
					unset($_SESSION['mid']);

					$menuitem = M('MenuItem');
					$fields = array('id', 'name', 'pid', 'path',"concat(path, '-', id)"=>'bpath');

					if($_GET['adds']!="custom"){
						$component = M('Component');
						$list = $component->find($_POST['cid']);
						$this->assign('list', $list);
						$item = $menuitem->field($fields)->order('bpath, id')->where('menuid='.$_POST['mid'])->select();
					}else{
						$item = $menuitem->field($fields)->order('bpath, id')->where('menuid='.$_GET['mid'])->select();

					}

					foreach($item as $key=>$value){
						$item[$key]['count'] = count(explode('-', $value['bpath']));
						for($i=0; $i<$item[$key]['count']; $i++){
							$item[$key]['nbsp'].= '&nbsp;&nbsp;&nbsp;&nbsp;';
						}
					}
					$this->assign('item', $item);

					$this->display('add');
				}else{
					$this->error('非法操作');
				}
			}else{
				$this->error('非法操作');
			}
		}else{
			$this->error('非法操作');
		}
	}

	//添加菜单
	function insert(){
		if((isset($_POST['menuid']) && !empty($_POST['menuid']))){
			if(isset($_POST['componentid']) && !empty($_POST['componentid'])){
				$menuitem = D('MenuItem');
				if($list = $menuitem->create()){
				//	dump($list);
					if(isset($_SESSION['miid']) && !empty($_SESSION['miid'])){
						if($menuitem->add()){
							$this->success('重写菜单项成功', __APP__.'/MenuItem/index/mid/'.$_POST['menuid']);
						}else{
							$this->error('重写菜单项失败');
						}
					}else{
						if($menuitem->add()){
							$this->success('添加菜单项成功', __APP__.'/MenuItem/index/mid/'.$_POST['menuid']);
						}else{
							$this->error('添加菜单项失败');
						}
					}
				}else{
					$this->error($menuitem->getError());
				}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}

	}

	//删除分组
	function del(){
		if(isset($_POST['mid']) && !empty($_POST['mid'])){
			if(!empty($_POST['del'])){
				$menuitem = M('MenuItem');
				foreach($_POST['del'] as $key=>$value){
					$count = $menuitem->where('menuid='.$_POST['mid'].' AND '.'pid='.$value)->count();
					if($count>0){
						$this->error('此菜单项下还有子菜单项,不能删除');
					}else{
						$num = $menuitem->delete($value);
						if($num>0){
							$this->success('删除菜单项成功');
						}else{
							$this->error('删除菜单项失败');
						}
					}

				}

			}else{
				$this->error('请选择菜单项');
			}
		}else{
			$this->error('异常操作');
		}

	}

	//指向修改分组界面
	function edit(){
		if($_GET['mid'] && !empty($_GET['mid'])){
			if(isset($_GET['id']) && !empty($_GET['id'])){
			$menuitem = M('MenuItem');
			$list = $menuitem->where('menuid='.$_GET['mid'])->find($_GET['id']);
				$fields = array('id', 'name', 'pid', 'path',"concat(path, '-', id)"=>'bpath');
				$item = $menuitem->field($fields)->order('bpath, id')->where('menuid='.$_GET['mid'])->select();
				foreach($item as $key=>$value){
					$item[$key]['count'] = count(explode('-', $value['bpath']));
					for($i=0; $i<$item[$key]['count']; $i++){
						$item[$key]['nbsp'].= '&nbsp;&nbsp;&nbsp;&nbsp;';
					}
				}
		//		dump($item);
		//		dump($list);
			$this->assign('item', $item);
			$this->assign('list', $list);
			$this->display();
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}

	function component(){
		if(isset($_GET['mid']) && !empty($_GET['mid'])){
			if(isset($_GET['miid']) && !empty($_GET['miid'])){
				$_SESSION['miid'] = $_GET['miid'];
			}else{
				unset($_SESSION['miid']);
			}
			$component = M('Component');
			$list = $component->where('published=1')->select();
			if(!empty($list)){
				$this->assign('list', $list);
				$this->display();
			}else{
				$this->assign('isshow', 'no');
				$this->assign('nobody', 'no');
				$this->display();
			}
		}else{
			$this->error('异常操作');
		}

	}

	//修改分组
	function update(){
		if(isset($_POST['id']) && !empty($_POST['id'])){
			if(isset($_POST['mid']) && !empty($_POST['mid'])){
			$menuitem = D('MenuItem');
		//		$datas = $menuitem->where('menuid='.$_POST['mid'])->find($_POST['id']);
		//		dump($datas);
				$count = $menuitem->where('menuid='.$_POST['mid'].' AND '.'pid='.$_POST['id'])->count();
		//		dump($count);

			if($list = $menuitem->create()){


				if($list['pid']==$list['id']){
					unset($list['pid']);
					unset($list['path']);
				}else{
					if($count>0){
						$this->error('此菜单项下还有子菜单项,不能修改位置');
					}
				}
		//		dump($list);
				if($menuitem->save($list)){
					$this->success('修改菜单项成功', __APP__.'/MenuItem/index/mid/'.$_POST['mid']);
				}else{
					$this->error('修改菜单项失败');
				}
			}else{
				$this->error($menuitem->getError());
			}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}

}